<template>
  <div class="resource-category">
    <div class="resource-category-list">
        <el-card class="box-card">
        <el-card class="box-card add-categray" :body-style="{ padding: '0' }">
            <el-button size="mini" @click="openAddCate">添加</el-button>
        </el-card>
        <el-table
            :data="categoryList"
            style="width: 100%; margin-bottom: 20px"
            v-loading="isLoading"
        >
            <el-table-column type="index" label="编号" width="100"></el-table-column>
            <el-table-column prop="name" label="资源名称" width="240"></el-table-column>
            <el-table-column prop="url" width="240" label="资源路径"></el-table-column>
            <el-table-column prop="description" width="240" label="描述"></el-table-column>
            <el-table-column prop="createdTime" label="添加时间"></el-table-column>
            <el-table-column width="180" label="操作">
            <template slot-scope="scope">
                <el-button
                size="mini"
                @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)">删除</el-button>
            </template>
            </el-table-column>
        </el-table>
        </el-card>
    </div>

    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form">
        <el-form-item label="名称" :label-width="'50px'">
            <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="'50px'">
            <el-input v-model="form.sort" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="onSaveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { setTime } from '@/utils/timer'
import { getResourceCategories, saveOrUpdateCategory, deleteResourceCategory } from '@/services/resource-category'

export default Vue.extend({
  name: 'ResourceCategoryIndex',
  data () {
    return {
      isLoading: true, // 加载状态
      totalCount: 0,
      categoryList: [],
      dialogVisible: false,
      isSavingOrUpdate: false,
      form: {
        id: '',
        name: '',
        sort: 0
      }
    }
  },
  created () {
    this.loadCategories()
  },
  methods: {
    async loadCategories () {
      this.isLoading = true // 展示加载中状态
      const { data } = await getResourceCategories()
      this.categoryList = data.data.map((item: any) => {
        item.createdTime = setTime(item.createdTime)
        return item
      })
      this.isLoading = false
    },
    async handleEdit (item: any) {
      this.form = {
        id: item.id,
        name: item.name,
        sort: item.sort
      }
      this.dialogVisible = true
    },
    async handleDelete (item: any) {
      this.$confirm('确认删除吗？', '删除提示', {})
        .then(async () => { // 确认执行这里
          // 请求删除操作
          const { data } = await deleteResourceCategory(item.id)
          if (data.code === '000000') {
            this.$message.success('删除成功')
            this.loadCategories() // 更新数据列表
          } else {
            this.$message.error('删除失败！')
          }
        })
        .catch(err => { // 取消执行这里
          console.log('err:', err)
          if (err === 'cancel') {
            this.$message.info('已取消删除')
          } else {
            this.$message.error('删除失败，请查看该分类下是否存在资源！')
          }
        })
    },
    openAddCate () {
      this.form = {
        id: '',
        name: '',
        sort: 0
      }
      this.dialogVisible = true
    },
    async onSaveOrUpdate () {
      if (!this.isSavingOrUpdate) {
        this.isSavingOrUpdate = true
        const { data } = await saveOrUpdateCategory(this.form)
        if (data.code === '000000') {
          this.dialogVisible = false
          this.$message.success('添加成功')
          this.loadCategories() // 更新数据列表
        } else {
          this.$message.info('添加失败，请重新添加！')
        }
        this.isSavingOrUpdate = false
      }
    }
  }
})
</script>

<style lang="scss" scoped>
.el-card.add-categray{
  box-shadow: none;
  border: none;
  border-bottom: 1px solid #EBEEF5;
  border-radius: 0;
  padding-bottom: 20px;
}
</style>
